<%@ page language="java" contentType="text/html"  pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<div class="form-group">
    <label class="col-sm-2 control-label">${pTitle}栏目：</label>	
    <div id="showSelectBox"></div>
	<div class="col-sm-2">
		<a href="javascript:getCateData(0);" style="position:relative;top:6px;">查看子栏目</a>
	</div>		    
</div>
<script>
    var initCategory=true;
    function checkCategory(id){
    	var key=${categoryId};
    	for(var i=0;i<key.length;i++){
    		if(key[i]==id){
    			return true;
    		}
    	}
    	return false;
    }
    
    function getCateData(){   	
    	var l=$("select[name='category_ids']").length;
    	var id=0;
    	if(l>0){
    		id=$("select[name='category_ids']")[l-1].value;
    	}
    	$.ajax({
    		 async:false,
    		 url:"${APP}admin/category/getCategory?id="+id,			 
    		 dataType: 'json',
 	         type:'get',	
    		 success:function(json){
    			 if(json.length==0 && initCategory){
    	    			initCategory=false;
    	    			return;
    	    		}
    	    		if(json.length==0){
    	    			alert.error("已经没有子栏目了!");
    	    			return;
    	    		}
    	    		str="<div class=\"col-sm-2\">";
    	    		str+="<span class=\"label-default badge\" onclick=\"clearNext(this,1);\" style=\"position:absolute;right: 5px;top:-8px;cursor:pointer;padding: 4px 4px;\"><i class=\"iconfont icon-delete size-12\"></i></span>";    		
    	    		str+="<select name=\"category_ids\" class=\"form-control\" onchange=\"clearNext(this);\">";
    	    		var check=0;
    	    		for(var i=0;i<json.length;i++){   
    	    			var b=checkCategory(json[i].id);
    	    			if(b){
    	    				check=json[i].id;
    	    				str+="<option selected=\"selected\" value=\""+json[i].id+"\">"+json[i].title+"</option>"; 
    	    			}else{
    	    				str+="<option value=\""+json[i].id+"\">"+json[i].title+"</option>"; 
    	    			}	
    	    		}  	
    	    		str+="</select>";	
    				str+="</div>";	 
    	    		$("#showSelectBox").append(str);
    		 }
    	});  	
    }  
    
    function clearNext(element,type){    	
    	var m=$($(element).parent()).nextAll();
    	m.remove();
		if(type==1){
			$(element).parent().remove();
    	}
    }   
    var sm=${categoryId};
    if(sm.length>0){
        for(var i=0;i<sm.length;i++){
        	getCateData();
        }
    }

    
</script>
